package me.heyclock.hl.recordwriter.tools;

import android.content.Context;
import android.media.MediaPlayer;

import java.io.IOException;
import java.util.HashMap;

/**
 * 媒体播放工具
 */
public class MediaPlayTool {
    public static HashMap<String, MediaPlayer> mediaPlayerList = new HashMap<String, MediaPlayer>();

    public static void init(String key, Context context, int resourceId){
        MediaPlayer mediaPlayer = MediaPlayer.create(context, resourceId);
        mediaPlayer.setLooping(true);
        mediaPlayerList.put(key, mediaPlayer);
    }

    public static void init(String key, Context context, String mediaSource){
        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(mediaSource);
            mediaPlayer.prepareAsync();
            mediaPlayerList.put(key, mediaPlayer);
        } catch (IOException e) {
            //e.printStackTrace();
        }
    }

    public static void play(String key){
        if (mediaPlayerList.containsKey(key)){
            mediaPlayerList.get(key).start();
        }
    }

    public static void pause(String key){
        if (mediaPlayerList.containsKey(key)){
            mediaPlayerList.get(key).pause();
        }
    }

    public static void release(String key){
        if (mediaPlayerList.containsKey(key)){
            mediaPlayerList.get(key).stop();
            mediaPlayerList.get(key).release();
        }
        mediaPlayerList.remove(key);
    }
}
